package cn.demoncat.util.zookeeper.entity;

import java.io.Serializable;
import java.util.Objects;

import org.apache.zookeeper.data.Stat;

import cn.demoncat.util.zookeeper.constant.NodeConstant;

/**
 * Zookeeper节点
 * 
 * @author 延晓磊
 *
 * @since 2018年11月6日
 */
public class ZkNodeInfo implements Serializable{
 
	private static final long serialVersionUID = 1L;
	
	// 完整路径
	private String id;
	// 节点名称
	private String name;
	// 父节点路径
	private String parentId;
	// 数据
	private String data;
	// 是否为父节点：stat.numChildren
	private Boolean isParent;
	// 节点模式：stat.ephemeralOwner
	private String mode;
	// 子节点数：stat.numChildren
	private int childNum;
	// 创建时间：stat.ctime
	private long createTime;
	// 更新时间：stat.mtime
	private long updateTime;
	// 更新版本：stat.dataVersion
	private int dataVersion;
	// 子节点更新版本：stat.cversion
	private int childVersion;
	
	/**
	 * 填充Stat信息
	 * 
	 * @param stat
	 * 
	 * @author 延晓磊
	 *
	 * @since 2018年11月6日
	 */
	public void setStat(Stat stat) {
		this.childNum = stat.getNumChildren();
		this.childVersion = stat.getCversion();
		this.createTime = stat.getCtime();
		this.updateTime = stat.getMtime();
		this.dataVersion = stat.getVersion();
		// 父节点：stat.numChildren > 0
		this.isParent = stat.getNumChildren() > 0;
		// 持久节点：stat.ephemeralOwner == 0；临时节点：stat.ephemeralOwner ==0 会话的sessionId
		this.mode = stat.getEphemeralOwner() == 0 ? NodeConstant.Mode.PERSISTENT : NodeConstant.Mode.EPHEMERAL;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getParentId() {
		return parentId;
	}
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}
	public Boolean getIsParent() {
		return isParent;
	}
	public void setIsParent(Boolean isParent) {
		this.isParent = isParent;
	}
	public String getMode() {
		return mode;
	}
	public void setMode(String mode) {
		this.mode = mode;
	}
	public int getChildNum() {
		return childNum;
	}
	public void setChildNum(int childNum) {
		this.childNum = childNum;
	}
	public long getCreateTime() {
		return createTime;
	}
	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}
	public long getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(long updateTime) {
		this.updateTime = updateTime;
	}
	public int getDataVersion() {
		return dataVersion;
	}
	public void setDataVersion(int dataVersion) {
		this.dataVersion = dataVersion;
	}
	public int getChildVersion() {
		return childVersion;
	}
	public void setChildVersion(int childVersion) {
		this.childVersion = childVersion;
	}
	@Override
	public String toString() {
		return "ZkNodeInfo [id=" + id + ", name=" + name + ", parentId=" + parentId + ", data=" + data + ", isParent="
				+ isParent + ", mode=" + mode + ", childNum=" + childNum + ", createTime=" + createTime
				+ ", updateTime=" + updateTime + ", dataVersion=" + dataVersion + ", childVersion=" + childVersion
				+ "]";
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		ZkNodeInfo that = (ZkNodeInfo) o;
		return childNum == that.childNum &&
				createTime == that.createTime &&
				updateTime == that.updateTime &&
				dataVersion == that.dataVersion &&
				childVersion == that.childVersion &&
				Objects.equals(id, that.id) &&
				Objects.equals(name, that.name) &&
				Objects.equals(parentId, that.parentId) &&
				Objects.equals(data, that.data) &&
				Objects.equals(isParent, that.isParent) &&
				Objects.equals(mode, that.mode);
	}

	@Override
	public int hashCode() {
		return Objects.hash(id, name, parentId, data, isParent, mode, childNum, createTime, updateTime, dataVersion, childVersion);
	}
}
